@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

echo.
echo ================================================================
echo                    Web Terminal Launcher
echo ================================================================
echo.

:: Check Python installation
echo [INFO] Checking Python environment...
python --version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Python not found, please install Python 3.7+
    echo [TIP] Download from: https://www.python.org/downloads/
    pause
    exit /b 1
)

:: Display Python version
for /f "tokens=2" %%i in ('python --version 2^>^&1') do set PYTHON_VERSION=%%i
echo [OK] Python version: %PYTHON_VERSION%

:: Check Docker environment
echo [INFO] Checking Docker environment...
docker version >nul 2>&1
if errorlevel 1 (
    echo [ERROR] Docker is not running or not installed
    echo [TIP] Please start Docker Desktop or install Docker
    pause
    exit /b 1
)
echo [OK] Docker environment is ready

:: Check current directory
if not exist "server.py" (
    echo [ERROR] server.py file not found
    echo [TIP] Please run this script in the web-terminal directory
    pause
    exit /b 1
)

:: Check Linux container status
echo [INFO] Checking Linux container status...
docker ps -a --filter "name=linux-basic" --format "table {{.Names}}\t{{.Status}}" | findstr "linux-basic" >nul
if errorlevel 1 (
    echo [WARN] linux-basic container not found
    echo [INFO] Creating and starting container...
    cd ..
    docker-compose up -d
    if errorlevel 1 (
        echo [ERROR] Failed to start container
        pause
        exit /b 1
    )
    cd web-terminal
    echo [OK] Container started successfully
) else (
    :: Check if container is running
    docker ps --filter "name=linux-basic" --format "table {{.Names}}\t{{.Status}}" | findstr "linux-basic" >nul
    if errorlevel 1 (
        echo [INFO] Starting existing container...
        docker start linux-basic
        if errorlevel 1 (
            echo [ERROR] Failed to start container
            pause
            exit /b 1
        )
        echo [OK] Container started successfully
    ) else (
        echo [OK] Container is already running
    )
)

:: Wait for container to fully start
echo [INFO] Waiting for container to fully start...
timeout /t 3 /nobreak >nul

:: Display connection information
echo.
echo ================================================================
echo                      Connection Information
echo ================================================================
echo.
echo  Web Terminal URL: http://localhost:8081
echo  Linux Container: localhost:2222
echo.
echo  Login Account:
echo    Username: developer
echo    Password: dev123
echo.
echo  Usage:
echo    1. Browser will automatically open the Web Terminal page
echo    2. Click "Connect SSH" button
echo    3. Enter password: dev123
echo    4. Start using Linux command line
echo.
echo ================================================================
echo.

:: Start Web server
echo [INFO] Starting Web Terminal server...
echo.

:: Delay opening browser
start "" cmd /c "timeout /t 3 /nobreak >nul && start http://localhost:8081"

:: Start Python server
python server.py

:: Cleanup after server stops
echo.
echo [INFO] Web Terminal server has stopped
echo.
pause